#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <error.h>
#include <time.h>
#include <arpa/inet.h>
#include <unistd.h>

#define MAXLINE 4096
#define LISTENQ 1024


int main(int argc, char *argv[])
{
  int listenfd, connfd;
  pid_t pid;
  struct sockaddr_in servaddr;
  char buff[MAXLINE] = "hell maple";
  time_t ticks;

  listenfd = socket(AF_INET, SOCK_STREAM, 0);

  bzero(&servaddr, sizeof(servaddr));

  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(3000);
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);

  bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));

  listen(listenfd, LISTENQ);

  while(1)
  {
    struct sockaddr_in client_addr;
    int addr_len = sizeof(client_addr);
    connfd = accept(listenfd, (struct sockaddr *)&client_addr, &addr_len);
    if((pid = fork())== 0)
    {
      char buf[MAXLINE];
      close(listenfd);
      printf("host: %s port: %d \n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
      write(connfd, buff, MAXLINE);
      close(connfd);
      exit(0);

    }
    close(connfd);

  }
  exit(0);
}